package net.shopxx.action.shop;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import net.shopxx.bean.Pager;
import net.shopxx.entity.Order;
import net.shopxx.entity.OrderLog;
import net.shopxx.entity.OrderLog.OrderLogType;
import net.shopxx.service.OrderItemService;
import net.shopxx.service.OrderLogService;
import net.shopxx.service.OrderPointerService;
import net.shopxx.service.OrderService;

import org.apache.struts2.convention.annotation.ParentPackage;

/**
 * 前台Action类 - 订单处理
 */

@ParentPackage("member")
public class OrderJobsAction extends BaseShopAction {

	private static final long serialVersionUID = 2553137844831167917L;
	private Order order;// 订单

	@Resource
	private OrderService orderService;
	@Resource
	private OrderLogService orderLogService;
	@Resource
	private OrderItemService orderItemService;


	@SuppressWarnings("unchecked")
	public String closeUnpaid() {
		if (pager == null) {
			pager = new Pager();
		}
		pager.addWhereCluase("orderStatus", Order.OrderStatus.unprocessed);
		pager.addWhereCluase("paymentStatus", Order.PaymentStatus.unpaid);
		pager.setPageSize(Integer.MAX_VALUE);
		pager = orderService.findByPager(pager);
		for (Order order : (List<Order>)pager.getList()) {
			if(Order.OrderStatus.unprocessed.equals(order.getOrderStatus())&& Order.PaymentStatus.unpaid.equals(order.getPaymentStatus()) )
			{
				// 订单日志
				OrderLog orderLog = new OrderLog();
				orderLog.setOrderLogType(OrderLogType.invalid);
				orderLog.setOrderSn(order.getOrderSn());
				orderLog.setOperator("系统");
				orderLog.setInfo("自动关闭未付款的订单");
				orderLog.setOrder(order);
				orderLogService.save(orderLog);
				if(order.getPayTime()==null)
				order.setOrderStatus(Order.OrderStatus.closed);
				orderService.update(order);
			}
		}
		return null;
	}
	

	@SuppressWarnings("unchecked")
	public String autoComfrimed() {
		if (pager == null) {
			pager = new Pager();
		}
		pager.addWhereCluase("orderStatus", Order.OrderStatus.processed);
		pager.addWhereCluase("paymentStatus", Order.PaymentStatus.paid);
		pager.addWhereCluase("shippingStatus", Order.ShippingStatus.shipped);
		pager.setPageSize(Integer.MAX_VALUE);
		pager = orderService.findByPager(pager);
		for (Order order : (List<Order>)pager.getList()) {
			if(order.getReceiveTime()==null)
			{
				order.setShippingStatus(Order.ShippingStatus.confirmed);
				// 订单日志
				OrderLog orderLog = new OrderLog();
				orderLog.setOrderLogType(OrderLogType.confirmed);
				orderLog.setOrderSn(order.getOrderSn());
				orderLog.setOperator("系统");
				orderLog.setInfo("系统自动确认收货");
				orderLog.setOrder(order);
				orderLogService.save(orderLog);
				orderService.update(order);
			}
		}
		return null;
	}

}